package io.vector.study.designpattern.factory.simple;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

/**
 * Created by vector on 2017/3/20.
 */
public class Calculator {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private IntegerPair integerPair;

    @Before
    public void prepareIntegerPair() {
        Random random = new Random();
        integerPair = new IntegerPair(random.nextInt(100), random.nextInt(100) + 1);
    }

    @Test
    public void testAdd() {
        executeTestCase("Add", "+");
    }

    @Test
    public void testSubtract() {
        executeTestCase("Subtract", "-");
    }

    @Test
    public void testMulti() {
        executeTestCase("Multi", "*");
    }

    @Test
    public void testDivision() {
        executeTestCase("Division", "/");
    }

    private void executeTestCase(String caseName, String operatorName) {
        BinaryOperator operator = SimpleOperatorFactory.getOperator(operatorName);
        Integer result = operator.calculate(integerPair);
        logger.info("case [{}]: {} {} {} = {}",
                caseName,
                integerPair.getLeft(),
                operatorName,
                integerPair.getRight(),
                result
        );
    }

}
